<?php

namespace App\Models\Admin;

use Zizaco\Entrust\EntrustRole;
use Zizaco\Entrust\Traits\EntrustRoleTrait;

class Role extends EntrustRole
{
    use EntrustRoleTrait;

    protected $fillable = ['name', 'display_name', 'description'];
    //
    /**
     * 查询角色并分页
     * @date   2016-11-02T15:17:24+0800
     * @param  [type]                   $start  [起始数目]
     * @param  [type]                   $length [读取条数]
     * @param  [type]                   $search [搜索数组数据]
     * @param  [type]                   $order  [排序数组数据]
     * @return [type]                           [查询结果集，包含查询的数量及查询的结果对象]
     */
    public function getRoleList($start,$length,$search,$order)
    {
        $role = $this;
        if ($search['value']) {
            if($search['regex'] == 'true'){
                $role = $role->where('name', 'like', "%{$search['value']}%")->orWhere('display_name','like', "%{$search['value']}%");
            }else{
                $role = $role->where('name', $search['value'])->orWhere('display_name', $search['value']);
            }
        }

        $count = $role->count();

        $role = $role->orderBy($order['name']);

        $roles = $role->offset($start)->limit($length)->get();

        return compact('count','roles');
    }
}
